<?php

namespace app\common\model\user;

use app\common\enum\ChannelEnum;
use tmcore\base\BaseModel;
use tmcore\traits\ModelTrait;

/**
 * 用户模型
 */
class User extends BaseModel
{
    use ModelTrait;

    /**
     * 用户密码加密
     * @param $password
     * @param $salt
     * @return string
     */
    public static function getPassword($password, $salt) {

        return md5(md5($password . $salt) . config('core.unique_identification'));
    }

    /**
     * 获取渠道id
     * @param $value
     * @param $data
     * @return string
     */
    public function getChannelIdsAttr($value, $data)
    {
        return UserAuthLog::where('uid', $data['id'])->field('id,uid,channel')->group('channel')->select();
    }

    /**
     * 获取渠道名称
     * @param $value
     * @param $data
     * @return string
     */
    public function getChannelNamesAttr($value, $data)
    {
        $names = '';
        $list = UserAuthLog::where('uid', $data['id'])->field('id,uid,channel')->order('id desc')->group('channel')->select();
        foreach ($list as $key => $value) {
            $names .= ',' . ChannelEnum::getChannelDesc($value['channel']);
        }
        return trim($names, ',');
    }

    /**
     * 获取分组名称
     * @param $value
     * @param $data
     * @return string
     */
    public function getGroupNameAttr($value, $data)
    {
        return UserGroup::where('id', $data['group_id'])->value('name');
    }

    /**
     * 性别描述
     * @param $value
     * @param $data
     * @return string
     */
    public function getSexDescAttr($value, $data)
    {
        if($data['sex'] == 1) {
            return '男';
        } elseif ($data['sex'] == 2) {
            return '女';
        } else {
            return '未知';
        }
    }

    /**
     * 状态描述
     * @param $value
     * @param $data
     * @return string
     */
    public function getStatusDescAttr($value, $data)
    {
        return $data['status'] ? '正常' : '禁止';
    }

    /**
     * 最后登录时间描述
     * @param $value
     * @param $data
     * @return string
     */
    public function getLastTimeDescAttr($value, $data)
    {
        return $data['last_time']?date('Y-m-d H:i:s', $data['last_time']):'';
    }
}